﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Other
{
    /// <summary>
    ///     公告管理
    /// </summary>
    [Access("258004")]
    public partial class Announce : WebFactory.PageBase
    {
        private readonly AnnounceManage Bll = PolicyInjection.Create<AnnounceManage>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var Action = Request["Action"];
                if (Action == "Add")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = false;
                }
                else if (Action == "Edit")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = true;
                    this.btnOK.Visible = false;


                    var model = new AnnounceManage().GetInfo(Request["ID"]);
                    this.txtTitle.Text = model.Title;
                    this.cbkPass.Checked = model.Pass;
                    this.CC_Content.Text = model.Content;
                }

                else
                {
                    BindList(1);
                }
            }
        }

        private void BindList(int pageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);
            var pp = new PagingParam();
            pp.PageNo = pageNo;
            pp.PageSize = this.PageNavigation1.PageSize;
            this.GV_List.DataSource = this.Bll.BindList(null, pp);
            this.GV_List.DataBind();

            this.PageNavigation1.PageSize = pp.PageSize;

            if (this.GV_List.Rows.Count <= 0)
            {
                this.DelSeletedID.Enabled = false;
            }
        }

        protected void DelSeletedID_Click(object sender, EventArgs e)
        {
            var res = this.Bll.Delete(GetSelectedRow.GetIDs(this.GV_List));
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
                return;
            }
            BindList(1);
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var model = new WinnerCMS.Model.Announce
            {
                Title = this.txtTitle.Text.Trim(),
                Author = UserManage.Current.UserName,
                Content = this.CC_Content.Text,
                Pass = this.cbkPass.Checked
            };


            var res = this.Bll.Add(model);
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this);
            }
            else
            {
                MsgBox.Message("添加成功！", "Announce.aspx", this);
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var model = this.Bll.GetInfo(Request["ID"]);

            model.Title = this.txtTitle.Text;
            model.Content = this.CC_Content.Text;
            model.Pass = this.cbkPass.Checked;

            var res = this.Bll.Edit(model);
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this);
            }
            else
            {
                MsgBox.Message("修改成功！", "Announce.aspx", this);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Announce.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                this.Bll.PassOrUnpass(this.GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString());

                BindList(this.PageNavigation1.PageNo);
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var res = this.Bll.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString());
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
                return;
            }

            BindList(1);
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var tc = e.Row.Cells[4];
                var lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "True")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }
    }
}